package main

import (
	"fmt"
	"net"
	"time"
)

func main() {
	socket, err := net.DialUDP("udp", nil, &net.UDPAddr{
		IP:   net.IPv4(0, 0, 0, 0),
		Port: 3000,
	})
	if err != nil {
		fmt.Println("connect server failed, error:", err.Error())
		return
	}
	defer socket.Close()

	for {
		sendData := []byte("Heloo server")
		_, err = socket.Write(sendData)
		if err != nil {
			fmt.Println("send data failed, err:", err.Error())
			return
		}
		// receive data
		data := make([]byte, 4096)
		n, remoteAddr, err := socket.ReadFromUDP(data)
		if err != nil {
			fmt.Println("receive data failed, err:", err)
			return
		}
		fmt.Printf("recv:%v addr:%v count:%v\n", string(data[:n]), remoteAddr, n)
		time.Sleep(1 * time.Second)
	}
}
